from django.db import models

from apps.oaauth.models import OAUser, OADepartment


# Create your models here.
class Inform(models.Model):
    title = models.CharField(max_length=100, verbose_name="通知标题")
    content = models.TextField(verbose_name="通知内容")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建通知时间")
    # 约定：如果前端传的department中包含了[0],那么就认为这个通知是所有部门可见
    public = models.BooleanField(default=False, verbose_name="是否公开")
    author = models.ForeignKey(OAUser, on_delete=models.CASCADE, related_name="informs", related_query_name="informs",verbose_name="通知人")
    departments = models.ManyToManyField(OADepartment, related_name="informs", related_query_name="informs", verbose_name="通知部门")
    class Meta:
        ordering = ['-create_time']

class InformRead(models.Model):
    inform = models.ForeignKey(Inform, on_delete=models.CASCADE, related_name="reads", related_query_name="reads", verbose_name="通知")
    user = models.ForeignKey(OAUser, on_delete=models.CASCADE, related_name="reads", related_query_name="reads", verbose_name="收件人")
    read_time = models.DateTimeField(auto_now_add=True, verbose_name="阅读时间")

    class Meta:
        unique_together = ('inform', 'user')